package com.controller;


import com.beans.ReadingData;
import com.server.ReadingDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;


@Controller
public class ReadingDataController {
    private final ReadingDataService readingDataService;

    @Autowired
    public ReadingDataController(ReadingDataService readingDataService) {
        this.readingDataService = readingDataService;
    }

    @GetMapping("/readingdata/all")
    public String getAllReadingData(Model model) {
        List<ReadingData> readingDataList = readingDataService.getAllReadingData();
        model.addAttribute("readingDataList", readingDataList);
        return "readingDataList";
    }

    @GetMapping("/readingdata/{id}")
    public String getReadingDataById(@PathVariable int id, Model model) {
        ReadingData readingData = readingDataService.getReadingDataById(id);
        if (readingData != null) {
            model.addAttribute("readingData", readingData);
            return "readingDataDetails";
        } else {
            return "notFound";
        }
    }
}
